package com.coin;

import com.coin.dao.plugins.SqlMonitorManager;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Created by chenhao on 2017/3/28.
 * 配置多个SqlSessionFactory及其事务管理器
 */
@Configuration
@MapperScan(basePackages ="com.coin.dao.flow", sqlSessionFactoryRef = "flowSqlSessionFactory")
public class MyBatisFlowConfig {




    @Bean(name = "flowSqlSessionFactory")
    public SqlSessionFactoryBean accountingSlaveSqlSessionFactory(@Qualifier("flowDataSource")DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.coin.dao.model");

        SqlMonitorManager sqlMonitorManager = new SqlMonitorManager();
        sqlMonitorManager.setShowSql(true);
        sqlSessionFactoryBean.setPlugins(new SqlMonitorManager[]{sqlMonitorManager});

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:com/coin/dao/flow/*Mapper.xml"));
        return sqlSessionFactoryBean;
    }



    @Bean(name = "flowTransactionManager")
    public DataSourceTransactionManager accountingSlaveTransactionManager(@Qualifier("flowDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
